package logic

import (
	"context"
	"fmt"
	"wechat1/models/mysql"
	"wechat1/models/redis"

	"wechat1/wechat1rpc/internal/svc"
	"wechat1/wechat1rpc/wechat1rpc"

	"github.com/zeromicro/go-zero/core/logx"
)

type LoginLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewLoginLogic(ctx context.Context, svcCtx *svc.ServiceContext) *LoginLogic {
	return &LoginLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

func (l *LoginLogic) Login(in *wechat1rpc.LoginRequest) (*wechat1rpc.LoginResponse, error) {
	// todo: add your logic here and delete this line
	get := redis.Client.Get("codelogin" + in.Mobile)
	if get.Val() != in.Code {
		return nil, fmt.Errorf("短信验证码获取失败，请重新输入")
	}
	var FindWechat mysql.Wechat
	mysql.Db.Debug().Where("mobile", in.Mobile).Find(&FindWechat)
	if FindWechat.ID == 0 {
		return nil, fmt.Errorf("用户已注册，请重新输入")
	}
	if FindWechat.Password != in.Password {
		return nil, fmt.Errorf("密码不一致，请重新输入")
	}
	return &wechat1rpc.LoginResponse{Id: int64(FindWechat.ID)}, nil
}
